<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2023/6/17
 * Time: 15:19
 */
namespace app\admin\controller;

use app\middleware\AdminCheck;
use think\facade\Db;

class City {

    protected $middleware = [
        AdminCheck::class => ['except' 	=> [] ]
    ];


    /**
     *   @SWG\Post(
     *     path="/admin/city/getList",
     *     tags={"后台城市相关"},
     *     summary="获取城市列表",
     *     consumes={"application/json"},
     *     produces={"application/json"},
     *     @SWG\Parameter(
     *      in="query",
     *      required=true,
     *      name="",
     *      type="string",
     *      description="不需要参数"),
     *     @SWG\Response(response="200",description="ok")
     *  )
     */
    /**
     * 城市列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getList(){
        $param = input();
        $where = [];
        if(isset($param['is_hot']) && $param['is_hot'] !== ''){
            $where[] = ['is_hot','=',$param['is_hot']];
        }
        if(isset($param['region_name']) && $param['region_name'] !== ''){
            $where[] = ['region_name','=',$param['region_name']];
        }
        $list = Db::name('region')
            ->where($where)
            ->select();

        return success('城市列表',['list'=>$list]);
    }



    /**
     *   @SWG\Post(
     *     path="/admin/city/setHot",
     *     tags={"后台城市相关"},
     *     summary="设置和取消热门城市",
     *     consumes={"application/json"},
     *     produces={"application/json"},
     *     @SWG\Parameter(
     *      in="query",
     *      required=true,
     *      name="id",
     *      type="string",
     *      description="id"),
     *     @SWG\Response(response="200",description="ok")
     *  )
     */
    /**
     * 设置和取消热门城市
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function setHot(){
        validateParam('id','id');
        //首先判断现在是否是热门城市
        $city = Db::name('region')
            ->where('region_id',input('id'))
            ->find();
        if($city['is_hot'] === 1){
            //说明是热门城市
            Db::name('region')
                ->where('region_id',input('id'))
                ->update(['is_hot'=>0]);
            return success('取消热门城市');
        }else{
            //不是热门城市
            Db::name('region')
                ->where('region_id',input('id'))
                ->update(['is_hot'=>1]);
            return success('设置热门城市');
        }
    }


    /**
     * 开启或者关闭已开通城市
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function setOpen(){
        validateParam('id','id');
        //首先判断现在是否是热门城市
        $city = Db::name('region')
            ->where('region_id',input('id'))
            ->find();
        if($city['is_open'] === 1){
            //说明是热门城市
            Db::name('region')
                ->where('region_id',input('id'))
                ->update(['is_open'=>0]);
            return success('取消成功');
        }else{
            //不是热门城市
            Db::name('region')
                ->where('region_id',input('id'))
                ->update(['is_open'=>1]);
            return success('设置成功');
        }
    }


}